<template>
	<view v-if="cateList!=''">
		<!-- my-search是自定义组件 -->
		<!-- <my-search :bgColor="#000000" :radius="3"></my-search> -->
		<view @click="gotoSearch"><my-search ></my-search></view> 
		<view class="scroll-view-container">
			<!-- 左侧滑块区 -->
			<scroll-view class="left-scroll-view" scroll-y="true" :style="{height:windowHeight+'px' }">
				<block v-for="(item,index) in cateList" :key="index">
					<view :class="['left-scroll-view-item',index == active ? 'active' : '']" @click="activeChanged(index)">{{item.cat_name}}</view>		
				</block>
				
			</scroll-view>
			<!-- 右侧滑块区 -->
			<scroll-view class="right-scroll-view" scroll-y="true" :style="{height:windowHeight+'px' }" :scroll-top="scrollTop">
				<view class="cate-lv2" v-for="(item2,index2) in cateLevel2" :key="index2">
					<view class="cate-lv2-title">
						  {{item2.cat_name}} >
					</view>
					<!-- 第三层盒子 -->
					<view class="cate-lv3-list">
						<view class="cate-lv3-list-item" v-for="(item3,index3) in cateLevel3" :key="index3" @click="gotoGoodsList(item3)">
							<!-- 图片 -->
							<image :src="item3.cat_icon" mode=""></image>
							<text>{{item3.cat_name}}</text>
						</view>			
					</view>
				</view>
				<!-- 回到顶部的图标 -->
				<image class="blackTop"  src="../../static/tab_icons/fanhuidingbu.png" mode="" @click="blackTop"></image>
			</scroll-view>
		</view>
		
	</view>
</template>

<script>
	import badgeMix from'@/mixins/tabbar-badge.js'
	export default {
		mixins:[badgeMix],
	
		data() {
			return {
				//当前设备的可用高度
				windowHeight: 0,
				// 分类数据列表
				cateList: [],
				//激活项
				active: 0,
				//右侧二级分类
				cateLevel2: [],
				//右侧三级分类
				cateLevel3: [],
				//设置滚动条到顶部的距离
				scrollTop: 0
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			// console.log(sysInfo)
			this.windowHeight = sysInfo.windowHeight -50
			 // 调用获取分类列表数据的方法
			this.getCateList()
		},
		methods:{
			async getCateList(){
			// 发起请求
			    const { data: res } = await uni.$http.get('/api/public/v1/categories')
			    // 判断是否获取失败
			    if (res.meta.status !== 200) return uni.$showMsg()
			    // 转存数据
			    this.cateList = res.message
				//二级分类赋值
				this.cateLevel2 = res.message[0].children
				this.cateLevel3 = res.message[0].children[0].children
			},
			/**
			 * @param {Object} index
			 * 切换左侧一级菜单
			 */
			activeChanged(index){
				
				// console.log(index)当前索引
				this.active = index
				//重新为右侧二级分类赋值
				this.cateLevel2 = this.cateList[index].children
				//重新为右侧三级分类赋值
				this.cateLevel3 = this.cateList[index].children[index].children
				//当点击左侧一级菜单时候 把scrollTop赋值为 1和0之间切换 因为如果一直为同个数 他是不会切换的
				this.scrollTop = this.scrollTop === 0 ? 1: 0
			},
			/**
			 * 回到顶部
			 */
			blackTop(){
				this.scrollTop = this.scrollTop === 0 ? 1: 0
				wx.showToast({
				    title: '已回到顶部',  // 标题
				    icon: 'none',   // 图标类型，默认success
				    duration: 1000   // 提示窗停留时间，默认1500ms
				})
			},
			/**
			 * 跳转到商品列表页面
			 * @param {Object} item3
			 */
			gotoGoodsList(item3){
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid='+item3.cat_id
				})
			},
			gotoSearch(){
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
				
			}
			
		}
	}
</script>

<style lang="scss">
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 120px;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #F4F4F4;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: ' ';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 25%
        }
      }
    }
  }
}
.cate-lv2-title{
	font-size: 14px;
	font-weight: bold;
	padding: 15px 0px;
}
.cate-lv3-list{
	display: flex;
	flex-wrap: wrap;//一行不够就换行
	.cate-lv3-list-item{
		width: 33.3%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		image{
			width: 60px;
			height: 60px;
		}
		text{
			font-size: 12px;
		}
	}
}
.blackTop{
	float: right;
	margin: 0px 20px 20px 0px;
	width: 30px;
	height: 30px;
}
</style>
